package com.ruoyi.business.controller;

import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.TbCompany;
import com.ruoyi.business.domain.TbCpadUser;
import com.ruoyi.business.domain.UserVO;
import com.ruoyi.business.service.ITbCompanyService;
import com.ruoyi.business.service.ITbCpadUserService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.TbResource;
import com.ruoyi.business.service.ITbResourceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资源上传记录Controller
 *
 * @author jkp
 * @date 2024-05-05
 */
@RestController
@RequestMapping("/business/resource")
public class TbResourceController extends BaseController
{
    @Autowired
    private ITbResourceService tbResourceService;

    @Autowired
    private ITbCpadUserService tbCpadUserService;

    @Autowired
    private ITbCompanyService tbCompanyService;

    /**
     * 查询资源上传记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:resource:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbResource tbResource)
    {
        startPage();
        List<TbResource> list = tbResourceService.selectTbResourceList(tbResource);
        return getDataTable(list);
    }

    /**
     * 导出资源上传记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:resource:export')")
    @Log(title = "资源上传记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbResource tbResource)
    {
        List<TbResource> list = tbResourceService.selectTbResourceList(tbResource);
        ExcelUtil<TbResource> util = new ExcelUtil<TbResource>(TbResource.class);
        util.exportExcel(response, list, "资源上传记录数据");
    }

    /**
     * 获取资源上传记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:resource:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbResourceService.selectTbResourceById(id));
    }

    /**
     * 新增资源上传记录
     */
    @PreAuthorize("@ss.hasPermi('business:resource:add')")
    @Log(title = "资源上传记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbResource tbResource)
    {
        return toAjax(tbResourceService.insertTbResource(tbResource));
    }

    /**
     * 修改资源上传记录
     */
    @PreAuthorize("@ss.hasPermi('business:resource:edit')")
    @Log(title = "资源上传记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbResource tbResource)
    {
        return toAjax(tbResourceService.updateTbResource(tbResource));
    }

    /**
     * 删除资源上传记录
     */
    @PreAuthorize("@ss.hasPermi('business:resource:remove')")
    @Log(title = "资源上传记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbResourceService.deleteTbResourceByIds(ids));
    }


    /**
     * 导入资源
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long companyId) throws Exception
    {
        ExcelUtil<TbCpadUser> util = new ExcelUtil<>(TbCpadUser.class);
        List<TbCpadUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();

        //查询公司名称
        TbCompany tbCompany = tbCompanyService.selectTbCompanyById(companyId);
        if (tbCompany==null){
            throw new ServletException("公司不存在");
        }

        TbResource tbResource = new TbResource();
        tbResource.setMonth(DateUtils.dateTimeNow("yyyy-MM"));
        tbResource.setFileName(file.getOriginalFilename());
        tbResource.setStatus(0);
        tbResource.setCompanyId(companyId);
        tbResource.setCompanyName(tbCompany.getName());
        //todo
//        tbResource.setRightNum(100L);
//        tbResource.setErrorNum(100L);

        tbResource.setCreateBy(operName);
        tbResourceService.insertTbResource(tbResource);

        String message = tbCpadUserService.importUser(userList, updateSupport, operName, tbCompany);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<UserVO> util = new ExcelUtil<>(UserVO.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
